package com.shopnight.utils;

import cn.hutool.core.date.DateUtil;
import com.shopnight.result.ApiResult;
import com.shopnight.vo.Goods;
import com.shopnight.vo.IndentInfoVO;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.text.MessageFormat;
import java.util.List;

public class EmailHtml {

    //下单后管理员邮件提醒
    public static String AdminEmailHtml(String imgurl,String younickneme,String sonnickname,String soncomment,String detailUrl) {
        String str = readFtl("templates/AdminMail.ftl");
        //替换html模板中的参数
        return MessageFormat.format(str, imgurl,younickneme,sonnickname,soncomment,detailUrl);
    }


    public static String ContentHtml(ApiResult apiResult){
        IndentInfoVO indentInfoVO = (IndentInfoVO) apiResult.getData();
        StringBuffer buffer = new StringBuffer();

        for (IndentInfoVO.GoodsVO goodsVO : indentInfoVO.getGoodsList()) {
            String str = readFtl("templates/MailContent.ftl");
            String redNum = goodsVO.getOrderNum() > 1 ?
                                       "<span style='color: red;font-size: 20px'>"+ goodsVO.getOrderNum() +"</span>":
                                       "<span>"+ goodsVO.getOrderNum() + "</span>";
            String formatStr = MessageFormat.format(str, goodsVO.getImgUrl(), goodsVO.getOrderName(), goodsVO.getPurchasePrice(), redNum);
            buffer.append(formatStr);
        }
        buffer.append("</hr>"); //横线
        buffer.append("<p>"+
                "收货人:"+    indentInfoVO.getConsignee()+                          "&nbsp&nbsp&nbsp"+
                "手机号:"+    indentInfoVO.getConsigneePhone() +                    "&nbsp&nbsp&nbsp"+
                "地址:"+      indentInfoVO.getDetailedAddress() +                   "&nbsp&nbsp&nbsp" +
                "</p>");
        buffer.append("<p>"+
                "订单金额:"+ indentInfoVO.getPrices()+                      "&nbsp&nbsp&nbsp"+
                "备注:"+    indentInfoVO.getRemark() +            "&nbsp&nbsp&nbsp"+
                "下单时间:"+ DateUtil.format( indentInfoVO.getCreateTime(), "yyyy-MM-dd HH:mm")  + "&nbsp&nbsp&nbsp"+
                "</p>");
        return  buffer.toString();
    }


    public static String RiderEmailHtml(String imgurl,String youname,String sonnickname,String soncomment) {
        String str = readFtl("templates/RiderMail.ftl");
        //替换html模板中的参数
        return MessageFormat.format(str, imgurl,youname,sonnickname,soncomment);
    }

    public static String readFtl(String path){
        //加载邮件html模
        Resource resource = new ClassPathResource(path);
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            System.out.print("发送邮件读取模板失败{}");
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  buffer.toString();
    }

}
